﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IHISCETeim.Model;
using IHISCETeim.BLL;
using Newtonsoft.Json.Linq;
using FineUI;
using System.Linq;
using System.Web.Security;
using System.Configuration;
using System.Text;
using System.Data;


namespace IHISCETeim.Web.AgentSys
{
    public partial class Default : AgentPageBase
    {
        protected override bool MustLogin
        {
            get
            {
                return true;
            }
        }

        private static Dictionary<string, string> _ConnStrInfo;
        /// <summary>
        /// 获取SQL联系字符串中的各个项,
        /// 项的标志全部为小写,并去掉项的空格符
        /// 如Data Source已变成datasource
        /// </summary>
        private static Dictionary<string, string> ConnStrInfo
        {
            get
            {
                ///Database DB = DatabaseFactory.CreateDatabase(SBOConnStr);
                if (_ConnStrInfo == null)
                {
                    string SBOconnstr = ConfigurationManager.ConnectionStrings["Connection String"].ConnectionString;
                    string[] connectionstring = SBOconnstr.Split(new char[] { ';' });
                    _ConnStrInfo = new Dictionary<string, string>();
                    foreach (string item in connectionstring)
                    {
                        string[] itemarr = item.Split(new char[] { '=' });
                        _ConnStrInfo.Add(itemarr[0].ToLower().Replace(" ", ""), itemarr[1]);
                    }
                }
                return _ConnStrInfo;
            }
        }

        protected static string GetDBName
        {
            get
            {
                string dbname = ConnStrInfo["initialcatalog"].ToString();
                if (dbname.ToLower() == "ihiscesys")
                    return "<span style='color:Lime'>(正式帐套)</span>";
                else
                    return "<span style='color:red'>(测试帐套)</span>";
            }
        }
        #region
        /// <summary>
        /// 获取当前用户对应的菜单信息
        /// </summary>
        /// <returns></returns>
        protected string GetMenu()
        {
            /*
                    <div class="accordionHeader">
                        <h2><span>Folder</span>系统管理</h2>
                    </div>					
                    <div class="accordionContent">
                        <ul class="tree">
                            <li><a href="SysBasic/OMeunList.aspx" target="navTab" rel="OMenuList">系统页面管理</a></li>	
                            <li><a href="SysBasic/ORoleList.aspx" target="navTab" rel="ORoleList">角色管理</a></li>							
                        </ul>
                    </div>
             */

            string menuhtml = ORoleController.DC.Get<string>(ORoleController.GetDataCacheKey(ORoleController.AgentRoleMenuById, User.RoleId));
            if (menuhtml != null)
            {
                return menuhtml;
            }

            StringBuilder sb = new StringBuilder();
            List<OMenuInfo> allmenudt = User.IsSuperAdmin ? OMenuController.GetAllOMenu2() : ORoleController.GetRoleMenu(User.RoleId);
            allmenudt = allmenudt.FindAll(e => e.IsUsed && e.IsAgent == true);//2014-07-17 加入菜单所属系统判断
            List<OMenuInfo> alltopmenu = allmenudt.FindAll(e => e.ParMenuId == 0 && e.IsUsed).OrderBy(e => e.DispayIndex).ToList();
            foreach (OMenuInfo topmenuitem in alltopmenu)
            {
                sb.AppendLine("<div class=\"accordionHeader\">");
                sb.AppendFormat("<h2><span>Folder</span>{0}</h2>", topmenuitem.MenuName);
                sb.AppendLine("</div>");

                sb.AppendLine("<div class=\"accordionContent\">");
                sb.AppendLine("<ul class=\"tree treeFolder\">");
                List<OMenuInfo> allsubmenu = allmenudt.FindAll(e => e.ParMenuId == topmenuitem.MenuId).OrderBy(e => e.DispayIndex).ToList();
                foreach (OMenuInfo submenuitem in allsubmenu)
                {
                    if (submenuitem.DWZTarget.ToLower() == "external")
                        sb.AppendFormat(" <li><a href=\"{0}\" target=\"{1}\" rel=\"{2}\" external=\"true\" class='tiptipClass' title=\"{5}\">{3}</a>{4}</li>", submenuitem.DWZHref, "navTab", submenuitem.DWZRel, submenuitem.MenuName, Get3Menu(submenuitem.MenuId), GetMenuChange(submenuitem.MenuId));
                    else
                        sb.AppendFormat(" <li><a href=\"{0}\" target=\"{1}\" rel=\"{2}\" fresh=\"{3}\" class='tiptipClass'  title=\"{6}\">{4}</a>{5}</li>", submenuitem.DWZHref, submenuitem.DWZTarget, submenuitem.DWZRel, submenuitem.DWZFlesh.ToString().ToLower(), submenuitem.MenuName, Get3Menu(submenuitem.MenuId), GetMenuChange(submenuitem.MenuId));

                }
                sb.AppendLine("</ul></div>");
            }
            //
            ORoleController.DC.Add(ORoleController.GetDataCacheKey(ORoleController.AgentRoleMenuById, User.RoleId), sb.ToString());
            //
            return sb.ToString();
        }

        protected string Get3Menu(int ParId)
        {
            List<OMenuInfo> allmenudt = User.IsSuperAdmin ? OMenuController.GetAllOMenu2() : ORoleController.GetRoleMenu(User.RoleId);
            allmenudt = allmenudt.FindAll(e => e.IsUsed);
            List<OMenuInfo> allsubmenu = allmenudt.FindAll(e => e.ParMenuId == ParId).OrderBy(e => e.DispayIndex).ToList();
            if (allsubmenu.Count <= 0)
                return "";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<ul>");
            foreach (OMenuInfo submenuitem in allsubmenu)
            {
                if (submenuitem.DWZTarget.ToLower() == "external")
                    sb.AppendFormat(" <li><a href=\"{0}\" target=\"{1}\" rel=\"{2}\" external=\"true\" class='tiptipClass' title=\"{5}\">{3}</a>{4}</li>", submenuitem.DWZHref, "navTab", submenuitem.DWZRel, submenuitem.MenuName, Get3Menu(submenuitem.MenuId), GetMenuChange(submenuitem.MenuId));
                else
                    sb.AppendFormat(" <li><a href=\"{0}\" target=\"{1}\" rel=\"{2}\" fresh=\"{3}\" class='tiptipClass'  title=\"{6}\">{4}</a>{5}</li>", submenuitem.DWZHref, submenuitem.DWZTarget, submenuitem.DWZRel, submenuitem.DWZFlesh.ToString().ToLower(), submenuitem.MenuName, Get3Menu(submenuitem.MenuId), GetMenuChange(submenuitem.MenuId));

                //sb.AppendFormat(" <li><a href=\"{0}\" target=\"{1}\" rel=\"{2}\" fresh=\"{3}\">{4}</a></li>", submenuitem.DWZHref, submenuitem.DWZTarget, submenuitem.DWZRel, submenuitem.DWZFlesh.ToString().ToLower(), submenuitem.MenuName);
            }
            sb.AppendLine("</ul>");
            return sb.ToString();
        }

        protected string GetHMenu()
        {
            /*
             <ul id="coolMenu">
		
			        <li><a href="#">系统管理</a></li>
			        <li><a href="#">公共查询</a></li>
			        <li>
				        <a href="#">整机管理系统</a>
				        <ul class="noJS">
					        <li><a href="#">作业日报表管理</a></li>
					        <li><a href="#">作业项目定义</a></li>
					        <li><a href="#">机器库存一浪表</a></li>
				        </ul>
			        </li>
			        <li><a href="#">Tyrio</a></li>
			        <li><a href="#">Quicumque</a></li>

		        </ul>
             */
            StringBuilder sb = new StringBuilder();
            List<OMenuInfo> allmenudt = User.IsSuperAdmin ? OMenuController.GetAllOMenu2() : ORoleController.GetRoleMenu(User.RoleId);
            allmenudt = allmenudt.FindAll(e => e.IsUsed);
            List<OMenuInfo> alltopmenu = allmenudt.FindAll(e => e.ParMenuId == 0 && e.IsUsed).OrderBy(e => e.DispayIndex).ToList();
            foreach (OMenuInfo topmenuitem in alltopmenu)
            {
                sb.AppendLine("<li>");
                sb.AppendFormat("<a href=\"#\">{0}</a>", topmenuitem.MenuName);
                sb.AppendLine("<ul class=\"noJS\">");
                List<OMenuInfo> allsubmenu = allmenudt.FindAll(e => e.ParMenuId == topmenuitem.MenuId).OrderBy(e => e.DispayIndex).ToList();
                foreach (OMenuInfo submenuitem in allsubmenu)
                {
                    sb.AppendFormat(" <li><a href=\"{0}\" target=\"{1}\" rel=\"{2}\" fresh=\"{3}\">{4}</a></li>", submenuitem.DWZHref, submenuitem.DWZTarget, submenuitem.DWZRel, submenuitem.DWZFlesh.ToString().ToLower(), submenuitem.MenuName);
                }
                sb.AppendLine("</ul></li>");
            }

            return sb.ToString();
        }

        private DataTable _menuchange = null;
        private DataTable MenuChange
        {
            get
            {
                if (_menuchange == null)
                    _menuchange = OMenuController.GetChangeAll();
                return _menuchange;
            }
        }
        protected string GetMenuChange(int MenuId)
        {
            try
            {
                return "";
                DataTable singChangeDT = MenuChange.Select(string.Format("MenuId={0}", MenuId)).CopyToDataTable();
                StringBuilder sb = new StringBuilder();
                sb.Append("<b>最后更新时间：</b><table>");
                foreach (DataRow dr in singChangeDT.Rows)
                {
                    sb.AppendFormat("<tr><td>{0}</td><td>{1}</td></tr>", dr["RealName"], Convert.ToDateTime(dr["LastUpdateDate"]).ToString("yyyy-MM-dd"));
                }
                sb.Append("</table>");
                return sb.ToString();
            }
            catch
            {
                return "";
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {

            if (OUserController.CurrentUser.UserName.ToLower() == OUserController.CurrentUser.PassWord.ToLower())
            {
                //需更换初始密码
                Response.Redirect(this.ResolveUrl("/MachineSys/UpdateInitPwd.aspx"));
            }
            OUserController.UpdateLastLogin(base.User.UserId, Request.UserHostAddress);

            // SyncSAPController.SyncUClaimDemage();
        }

       
    }
}